package com.zide.member.auth.controller;

import cn.hutool.core.util.ObjectUtil;
import com.zide.common.core.domain.CommonResult;
import com.zide.common.core.domain.model.MemberLoginUser;
import com.zide.common.utils.SecurityUtils;
import com.zide.member.auth.domain.vo.MemberUserLoginVO;
import com.zide.member.auth.service.IAuthLoginService;
import com.zide.framework.web.domain.vo.save.MemberRegisterVO;
import com.zide.member.auth.domain.vo.result.LoginResultVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import static com.zide.common.core.domain.CommonResult.*;

/**
 * @author 石添
 * @date 2024/2/26 17:38
 */
@Tag(name = "会员认证管理")
@RequestMapping("/member/auth")
@RestController
public class AuthMemberController {

    @Resource
    private IAuthLoginService authLoginService;

    @Operation(summary = "账号密码登录")
    @PostMapping("login")
    public CommonResult loginByAccountAndPassword(@RequestBody MemberUserLoginVO loginVO) {
        return success(authLoginService.loginByAccountAndPassword(loginVO));
    }

    @Operation(summary = "个人中心数据")
    @GetMapping("profile")
    public CommonResult searchProfile() {
        MemberLoginUser consumerUserInfo = SecurityUtils.getMemberLoginUser();
        Long consumerUserId = consumerUserInfo.getId();
        return success();
    }

    @Operation(summary = "账号注册")
    @PostMapping("account_register")
    public CommonResult registerByAccount(@RequestBody MemberRegisterVO registerVO) {
        LoginResultVO result = authLoginService.registerByAccount(registerVO);
        if(ObjectUtil.isNotNull(result)) {
           return success(result);
        }
        return error();
    }

    @Operation(summary = "退出登录")
    @PostMapping("logout")
    public CommonResult logout() {
        authLoginService.logout();
        return success();
    }
}
